package com.zhyt.intelligentpipeline.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhyt.intelligentpipeline.exception.CustomException;
import com.zhyt.intelligentpipeline.exception.ErrorCode;
import com.zhyt.intelligentpipeline.model.DeformationDetection;
import com.zhyt.intelligentpipeline.model.Pipeline;
import com.zhyt.intelligentpipeline.service.PipelineService;
import com.zhyt.intelligentpipeline.service.StationService;
import com.zhyt.intelligentpipeline.vo.PageVo;
import com.zhyt.intelligentpipeline.vo.PipelineWrap;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/pipeline")
public class PipelineController {


    @Resource
    private PipelineService pipelineService;

    @Resource
    private StationService stationService;


    @GetMapping
    public List<Pipeline> getPipelineInfo() {
        List<Pipeline> pipelines = pipelineService.getPipelines();
        if (pipelines == null || pipelines.size() == 0) {
            throw new NullPointerException("数据库出现异常");
        }
        return pipelines;
    }

    @GetMapping("{id}")
    public PipelineWrap getPipelineById(@PathVariable(name = "id")String id){
        Pipeline pipeline = pipelineService.getPipelineById(id);
        return PipelineWrap.pipelineToPipelineWrap(pipeline, stationService);

    }

    /**
     * 通过分页获取管段的信息
     * */
    @GetMapping("page/{page}/num/{num}")
    public List<Pipeline> getPipelineByPage(@PathVariable(value = "page")Integer pageNum,
                                            @PathVariable(value = "num")Integer num) {
        Page<Pipeline> page = new Page<>(pageNum, num);
        return pipelineService.getPipelinesByPage(page);
    }

    @GetMapping("pageInfo/num/{num}")
    public PageVo getPageInfo(@PathVariable(value = "num")Integer num){
        Long pipelineCounts = pipelineService.getPipelineCounts();
        PageVo pageVo = new PageVo();
        pageVo.setAllValues(pipelineCounts.intValue());
        pageVo.setEveryPageNums(num);
        pageVo.setPages((int)Math.ceil(pipelineCounts / (double) num));
        return pageVo;
    }

    /**
     * 添加管线信息
     * */
    @PostMapping
    public String addPipeline(@RequestBody Pipeline pipeline){
        Pipeline.checkValue(pipeline);
        return pipelineService.addPipeline(pipeline);
    }

    /**
     * 修改管线
     */
    @PutMapping     //是返回的Boolea类型
    public Boolean update(@RequestBody Pipeline pipeline)
    {
        return pipelineService.updateById(pipeline);
    }

    /**
     * 删除管线
     */
    @DeleteMapping("delete/{id}")//这里面的id是和形参中的id是相应的才能传过去
    public Boolean delete(@PathVariable String id)
    {
        return pipelineService.removeById(id);
    }

}
